<?php
header("Location:/");
die();

header("Content-type: text/html; charset=utf-8");

if (isWx()) {
?>
    请点击右上角选择用浏览器打开！
<?php
} else {
    $server_url = 'https://api.xiaomashengxian.com';
    $id = $_REQUEST['order_id'];
    $uid = $_REQUEST['uid'];
    $time = $_REQUEST['time'];
    $token = $_REQUEST['token'];
    if (empty($id) || empty($uid) || empty($time) || empty($token)) {
        die('非法访问！');
    }

    $url = $server_url . '/mobile/api/v1/Alipay.php?mode=payOrderInfo';
    $data = array(
        'order_id' => $id,
        'uid' => $uid,
        'time' => $time,
        'token' => $token
    );
    $rel = curlPost($url, $data);
    if ($rel['code'] != 200) {
        die('非法访问！');
    }

    $order_no = rsaEncrypt($rel['data']['order']['order_sn']);
    $amount     = rsaEncrypt($rel['data']['order']['order_amount']);
?>

    <form action="alipay/wappay/pay.php" id="pay_form" method="post" style="display: none;">
        <input type="test" name="order_no" value="<?php echo $order_no;?>">
        <input type="test" name="amount" value="<?php echo $amount;?>">
    </form>
    <script>
        document.getElementById('pay_form').submit();
    </script>

<?php
}

?>

<?php

/*
 * 模拟post请求
 */
function curlPost($url, $data) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // post数据
    curl_setopt($ch, CURLOPT_POST, 1);
    // post的变量
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    $output = curl_exec($ch);

    curl_close($ch);

    if( preg_match('/^\xEF\xBB\xBF/', $output) ) {
        $output = substr($output,3);
    }
    $output = json_decode(trim($output), true);

    return $output;
}

/*
 * 判断是否是微信浏览器
 */
function isWx() {
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
        return true;
    }

    return false;
}

/*
 * RSA 私钥加密
 */
function rsaEncrypt($decrypted) {
    $private_key = '-----BEGIN RSA PRIVATE KEY-----  
MIICXQIBAAKBgQC3//sR2tXw0wrC2DySx8vNGlqt3Y7ldU9+LBLI6e1KS5lfc5jl  
TGF7KBTSkCHBM3ouEHWqp1ZJ85iJe59aF5gIB2klBd6h4wrbbHA2XE1sq21ykja/  
Gqx7/IRia3zQfxGv/qEkyGOx+XALVoOlZqDwh76o2n1vP1D+tD3amHsK7QIDAQAB  
AoGBAKH14bMitESqD4PYwODWmy7rrrvyFPEnJJTECLjvKB7IkrVxVDkp1XiJnGKH  
2h5syHQ5qslPSGYJ1M/XkDnGINwaLVHVD3BoKKgKg1bZn7ao5pXT+herqxaVwWs6  
ga63yVSIC8jcODxiuvxJnUMQRLaqoF6aUb/2VWc2T5MDmxLhAkEA3pwGpvXgLiWL  
3h7QLYZLrLrbFRuRN4CYl4UYaAKokkAvZly04Glle8ycgOc2DzL4eiL4l/+x/gaq  
deJU/cHLRQJBANOZY0mEoVkwhU4bScSdnfM6usQowYBEwHYYh/OTv1a3SqcCE1f+  
qbAclCqeNiHajCcDmgYJ53LfIgyv0wCS54kCQAXaPkaHclRkQlAdqUV5IWYyJ25f  
oiq+Y8SgCCs73qixrU1YpJy9yKA/meG9smsl4Oh9IOIGI+zUygh9YdSmEq0CQQC2  
4G3IP2G3lNDRdZIm5NZ7PfnmyRabxk/UgVUWdk47IwTZHFkdhxKfC8QepUhBsAHL  
QjifGXY4eJKUBm3FpDGJAkAFwUxYssiJjvrHwnHFbg0rFkvvY63OSmnRxiL4X6EY  
yI9lblCsyfpl25l7l5zmJrAHn45zAiOoBrWqpM5edu7c  
-----END RSA PRIVATE KEY-----';

    $pi_key =  openssl_pkey_get_private($private_key);

    openssl_private_encrypt($decrypted, $encrypted, $pi_key);

    return base64_encode($encrypted);
}

?>
